package cn.sinata.wanglin.adapter;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Circle;
import cn.sinata.wanglin.model.Comment;
import cn.sinata.wanglin.model.Like;
import cn.sinata.wanglin.model.LikeData;
import cn.sinata.wanglin.neighbor.controller.FriendCircleListener;
import cn.sinata.wanglin.neighbor.view.CircleView;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

/**
 *
 * Created by liaoxiang on 16/12/13.
 */

public class CircleAdapter extends HFRecyclerAdapter<Circle> {

    private String userId;

    public CircleAdapter(List<Circle> mData) {
        super(mData, R.layout.item_circle_layout);
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
    }

    @Override
    public void onBind(int position, final Circle circle, ViewHolder holder) {
        CircleView circleView = holder.bind(R.id.circleView);
        circleView.update(position,circle);
        final int p = position;
        circleView.setListener(new FriendCircleListener() {

            @Override
            public void showAllToggle() {
                notifyDataSetChanged();
            }

            @Override
            public void onAvatarClick(int position, String id) {
                if (listener != null) {
                    listener.onAvatarClick(position,id);
                }
            }

            @Override
            public void onCommentButtonClick(int position, Comment comment) {
                if (listener != null) {
                    listener.onCommentButtonClick(position,comment);
                }
            }

            @Override
            public void onPraiseButtonClick(int position, String id) {
                praise(circle,id);
            }

            @Override
            public void onCommentItemClick(int position, Comment comment) {
                if (listener != null) {
                    //这里的position是评论列表的，不是圈子列表的，所以传p。
                    listener.onCommentItemClick(p,comment);
                }
            }

            @Override
            public void onLinkNameClick(String id) {

            }

            @Override
            public void onDelete(int position, String id) {
                deleteCircle(position,id);
            }
        });
    }

    private void praise(final Circle circle, String id){
        BaseActivity activity = null;
        if (mContext instanceof BaseActivity) {
            activity = (BaseActivity) mContext;
            activity.showDialog();
        }
        HttpManager.clickPraise(userId,"C",id)
                .subscribe(new ResultDataSubscriber<LikeData>(activity) {
                    @Override
                    public void onSuccess(String msg, LikeData likeData) {
                        showToast(msg);
                        //当前用户行为 1：取消点赞 0：点赞
                        if (likeData.getClickPraise() == 0) {
                            int num = circle.getLikeNum();
                            if (num < 4) {
                                circle.getLikeList().add(likeData.getLike());
                            }
                            num++;
                            circle.setLikeNum(num);
                            circle.setClickPraise(1);
                        } else {
                            int num = circle.getLikeNum();
                            List<Like> likes = circle.getLikeList();
                            for (Like like : likes) {
                                if (like.getUserID().equals(userId)) {
                                    likes.remove(like);
                                    break;
                                }
                            }
                            num--;
                            if (num < 0) {
                                num = 0;
                            }
                            circle.setLikeNum(num);
                            circle.setClickPraise(0);
                        }
                        notifyDataSetChanged();
                    }
                });
    }

    private void deleteCircle(final int position, String id){
        BaseActivity activity = null;
        if (mContext instanceof BaseActivity) {
            activity = (BaseActivity) mContext;
            activity.showDialog();
        }
        HttpManager.delSpoke(id,"C")
                .subscribe(new ResultDataSubscriber<JsonObject>(activity) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        mData.remove(position);
                        notifyDataSetChanged();
                    }
                });
    }

    private FriendCircleListener listener;

    public void setListener(FriendCircleListener listener) {
        this.listener = listener;
    }
}
